//$URL: http://dasding.googlecode.com/svn/trunk/api/src/main/java/de/piratech/dasding/api/rest/UserResource.java $
//$Id: UserResource.java 134 2013-04-19 21:23:53Z krieg.maria@gmail.com $
/*
 * Copyright 2012 Piratech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.piratech.dasding.api.rest;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import de.piratech.dasding.data.User;
import de.piratech.dasding.data.database.DatabaseException;
import de.piratech.dasding.data.helper.PasswordHelper;

/**
 * All actions for users
 * <p>
 * GET: -<br>
 * PUT: -<br>
 * POST: Creates an user<br>
 * DELETE: <br>
 *
 * @author deveth0
 * @since 0.4
 */
@Path("/users")
@Component
public class UserResource extends AbstractResource {

  private static final Logger LOG = LoggerFactory.getLogger(UserResource.class);

  
  
  @GET
  @Path("/find")
  @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
  public List<User> findUsers() {
    LOG.debug("findUsers()");
    try {
     return getUserData().getUsers();
    } catch (DatabaseException dbe) {
      throw getBadRequestException(dbe);
    }
  }
  
  /**
   * Creates a new user
   *
   * POST
   *
   * @param pName (required)
   * @param pFullname (required)
   * @param pPassword (required)
   * @param pMail (required)
   * @param pPhone
   * @param pMobile
   *
   * @return
   */
  @POST
  @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
  public Response createUser(
          @QueryParam("name") String pName,
          @QueryParam("fullname") String pFullname,
          @QueryParam("password") String pPassword,
          @QueryParam("phone") String pPhone,
          @QueryParam("mobile") String pMobile,
          @QueryParam("mail") String pMail) {
    LOG.debug("createUser(name={}, fullname={}, phone={}, mobile={}, mail={})", pName, pFullname, pPhone, pMobile, pMail);
    User user = new User();
    user.setName(pName);
    user.setFullname(pFullname);
    user.setPassword(PasswordHelper.getCryptedPassword(pPassword, pName));
    user.setMobile(pMobile);
    user.setMail(pMail);
    user.setPhone(pPhone);
    user.addRole(User.USER_ROLE.USER);
    if (!user.isValid()) {
      return getBadRequestResponse();
    }
    try {
      user = getUserData().createUser(user);
      return getCreatedResponse("/id/" + user.getName());
    } catch (DatabaseException dbe) {
      return getBadRequestResponse(dbe);
    }
  }

  @Override
  protected Logger getLogger() {
    return LOG;
  }
}
